package com.tony.queen;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.*;

/**
 * @author tonywang
 * @date 8/13/2018
 */
public class TestQueen {
//   static ArrayBlockingQueue fairQueue = new ArrayBlockingQueue(4,true);
//   static LinkedBlockingQueue fairQueue = new LinkedBlockingQueue(4);
   static PriorityBlockingQueue fairQueue = new PriorityBlockingQueue(4);
    public static void main(String[] args){
        ExecutorService executor = Executors.newFixedThreadPool(2);
        executor.submit(new Producer());
        executor.submit(new Consumer());
    }
    static class Producer implements Runnable {
        @Override
        public void run() {
            int count = 0;
            while (true){
                try {
                    fairQueue.put(count++);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("生产量:" + count + " 剩余量: " + fairQueue.size());
            }
        }
    }

    static class Consumer implements Runnable {

        @Override
        public void run() {
            int count = 0;
            while (true) {
                try {
                    fairQueue.take();
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count++;
                System.out.println("消费量:" + count + " 剩余量: " + fairQueue.size());
            }
        }
    }


}
